import { type KeyValueChecked } from "@sparrow/common/types/workspace";
import {
  TabPersistenceTypeEnum,
  TabTypeEnum,
  type Path,
} from "@sparrow/common/types/workspace/tab";
import {
  SocketDataTypeEnum,
  SocketSectionEnum,
  WebSocketDefault,
  type State,
  type WebSocketTab,
} from "@sparrow/common/types/workspace/web-socket";
import { v4 as uuidv4 } from "uuid";

class InitWebSocketTab {
  private generateWebSocketKey() {
    const array = new Uint8Array(16);
    window.crypto.getRandomValues(array);
    return btoa(String.fromCharCode.apply(null, array as unknown as number[]));
  }
  private _tab: WebSocketTab;
  /**
   *
   * @param _id - Request mongo id
   * @param _workspaceId - Workspace Id to which Request belongs to
   */
  constructor(_id: string, _workspaceId: string) {
    this._tab = {
      id: _id,
      tabId: uuidv4(),
      name: WebSocketDefault.NAME,
      type: TabTypeEnum.WEB_SOCKET,
      persistence: TabPersistenceTypeEnum.PERMANENT,
      description: "",
      source: "USER",
      isDeleted: false,
      activeSync: false,
      property: {
        websocket: {
          url: "",
          headers: [
            {
              key: "",
              value: "",
              checked: false,
            },
          ],
          queryParams: [
            {
              key: "",
              value: "",
              checked: false,
            },
          ],
          autoGeneratedHeaders: [
            // Commented out these auto-generated as we have moved to inbuilt websocket crate which handles these headers automatically.
            // {
            //   key: "Sec-WebSocket-Version",
            //   value: "13",
            //   checked: true,
            // },
            // {
            //   key: "Sec-WebSocket-Key",
            //   value: this.generateWebSocketKey(),
            //   checked: true,
            // },
          ],
          message: "",
          state: {
            socketNavigation: SocketSectionEnum.MESSAGE,
            socketMessageLanguage: SocketDataTypeEnum.TEXT,
            socketLeftSplitterWidthPercentage: 50,
            socketRightSplitterWidthPercentage: 50,
            isSaveSocketInProgress: false,
            isParameterBulkEditActive: false,
            isHeaderBulkEditActive: false,
          },
        },
      },
      path: {
        workspaceId: _workspaceId,
        collectionId: "",
        folderId: "",
      },
      isSaved: true,
      index: 0,
      isActive: true,
      timestamp: new Date().toString(),
    };
    if (!_id || !_workspaceId) {
      console.error("invalid id or workspace id on create new tab web socket!");
    }
  }
  public getValue(): WebSocketTab {
    return this._tab;
  }
  public updateId(_id: string) {
    this._tab.id = _id;
  }
  public updateTabType(type: TabPersistenceTypeEnum) {
    this._tab.persistence = type;
  }
  public updateName(_name: string) {
    this._tab.name = _name;
  }
  public updateDescription(_description: string) {
    this._tab.description = _description;
  }
  public updatePath(_path: Path) {
    this._tab.path = _path;
  }
  public updateUrl(_url: string) {
    if (_url) {
      this._tab.property.websocket.url = _url;
    }
  }
  public updateQueryParams(_queryParams: KeyValueChecked[]) {
    if (_queryParams) {
      this._tab.property.websocket.queryParams = _queryParams;
    }
  }
  public updateHeaders(_headers: KeyValueChecked[]) {
    if (_headers) {
      this._tab.property.websocket.headers = _headers;
    }
  }
  public updateMessage(_message: string) {
    this._tab.property.websocket.message = _message;
  }
  public updateAutoGeneratedHeaders(_autoGeneratedHeaders: KeyValueChecked[]) {
    this._tab.property.websocket.autoGeneratedHeaders = _autoGeneratedHeaders;
  }
  public updateIsSave(_isSave: boolean) {
    this._tab.isSaved = _isSave;
  }
  public updateState(_state: Partial<State>) {
    this._tab.property.websocket.state = {
      ...this._tab.property.websocket.state,
      ..._state,
    };
  }
}

export { InitWebSocketTab };
